//
//  UIVerticalLoopView.m
//  YYRXForClient
//
//  Created by 播利ios on 2016/12/8.
//  Copyright © 2016年 wujuntao. All rights reserved.
//

#import "UIVerticalLoopView.h"
#import "Vertical.h"

@implementation UIVerticalLoopView

- (id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

-(void)setupView {
    
    _firstContentLabel = [[Vertical alloc] initWithFrame:CGRectMake(12, 0, self.frame.size.width - 24, self.frame.size.height)];
    [_firstContentLabel setBackgroundColor:[UIColor clearColor]];
    [_firstContentLabel setNumberOfLines:0];
    _firstContentLabel.textAlignment=NSTextAlignmentLeft;
    _firstContentLabel.verticalAlignment=VerticalAlignmentMiddle;
    _firstContentLabel.userInteractionEnabled = YES;
    _firstContentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    [_firstContentLabel setTextColor:UIColorFromRGB(0x333333)];
    _firstContentLabel.font=[UIFont systemFontOfSize:12.f];
    
    UITapGestureRecognizer *tapGesturRecongnizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loopContentClick)];
    tapGesturRecongnizer1.numberOfTapsRequired = 1;
    [_firstContentLabel addGestureRecognizer:tapGesturRecongnizer1];
    
    //********
    _secondContentLabel = [[Vertical alloc] initWithFrame:CGRectMake(12, self.frame.size.height , self.frame.size.width - 24, self.frame.size.height)];
    [_secondContentLabel setBackgroundColor:[UIColor clearColor]];
    [_secondContentLabel setTextColor:UIColorFromRGB(0x333333)];
    _secondContentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    [_secondContentLabel setNumberOfLines:0];
    _secondContentLabel.verticalAlignment=VerticalAlignmentMiddle;
    _secondContentLabel.textAlignment=NSTextAlignmentLeft;
    _secondContentLabel.userInteractionEnabled = YES;
    _secondContentLabel.font=[UIFont systemFontOfSize:12.f];
    
    UITapGestureRecognizer *tapGesturRecongnizer2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loopContentClick)];
    tapGesturRecongnizer2.numberOfTapsRequired = 1;
    [_secondContentLabel addGestureRecognizer:tapGesturRecongnizer2];
    
    [self addSubview:_firstContentLabel];
    [self addSubview:_secondContentLabel];
    
    // 默认初始方向是向上
    _Direction = VerticalLoopDirectionDown;
    _verticalLoopAnimationDuration = 1;
    self.clipsToBounds = YES;
}

- (void)loopContentClick{
    if ([self.loopDelegate respondsToSelector:@selector(didClickContentAtIndex:)]) {
        [self.loopDelegate didClickContentAtIndex:currentIndex];
    }
}

-(void)startVerticalLoopAnimation{
    
    _firstContentLabel.text = _verticalLoopContentArr[currentIndex];
    
    float firstContentLaStartY = 0;
    float firstContentLaEndY = 0;
    float secondContentLaStartY = 0;
    float secondContentLaEndY = 0;
    
    int secondCurrentIndex  = currentIndex + 1;
    if (secondCurrentIndex > _verticalLoopContentArr.count - 1) {
        secondCurrentIndex = 0;
    }
    
    switch (_Direction) {
        case VerticalLoopDirectionBottom:
            
            firstContentLaStartY = 0;
            firstContentLaEndY = self.frame.size.height;
            
            secondContentLaStartY = firstContentLaStartY - self.frame.size.height;
            secondContentLaEndY = firstContentLaEndY - self.frame.size.height;
            
            break;
        case VerticalLoopDirectionDown:
            
            firstContentLaStartY = 0;
            firstContentLaEndY = -self.frame.size.height;
            
            secondContentLaStartY = firstContentLaStartY + self.frame.size.height;
            secondContentLaEndY = firstContentLaEndY + self.frame.size.height;
            
            break;
        default:
            break;
    }
    
    _secondContentLabel.text = _verticalLoopContentArr[secondCurrentIndex];
    _firstContentLabel.frame = CGRectMake(12, firstContentLaStartY, self.frame.size.width - 20, self.frame.size.height);
    _secondContentLabel.frame = CGRectMake(12, secondContentLaStartY, self.frame.size.width - 20, self.frame.size.height);
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //更新UI操作
        [UIView beginAnimations:@"" context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        [UIView setAnimationDuration:_verticalLoopAnimationDuration];
        [UIView setAnimationDelay:3];
        [UIView setAnimationDelegate:self];
        
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
        
        CGRect firstContentLabelFrame = _firstContentLabel.frame;
        firstContentLabelFrame.origin.y = firstContentLaEndY;
        
        [_firstContentLabel setFrame:firstContentLabelFrame];
        [_secondContentLabel setFrame:CGRectMake(12,secondContentLaEndY, self.frame.size.width - 20, self.frame.size.height)];
        [UIView commitAnimations];
    });

}

-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
    
    currentIndex++;
    if(currentIndex >= [_verticalLoopContentArr count]) {
        currentIndex = 0;
    }
    [self startVerticalLoopAnimation];
}

#pragma mark - verticalLoop Animation Handling
-(void)start {
    // 开启动画默认第一条信息
    currentIndex = 0;
    // 开始动画
    [self startVerticalLoopAnimation];
}

@end
